package com.zfzs.post.service.impl;

import com.zfzs.post.enums.GoodsType;
import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.kuaidi.KuaiDiUtils;
import com.zfzs.post.model.vo.GoodsListVO;
import com.zfzs.post.model.vo.GoodsVO;
import com.zfzs.post.modules.agent.service.dto.AgentDTO;
import com.zfzs.post.modules.goods.domain.GoodOrder;
import com.zfzs.post.modules.goods.domain.Goods;
import com.zfzs.post.modules.goods.service.GoodOrderService;
import com.zfzs.post.modules.goods.service.GoodsService;
import com.zfzs.post.modules.post.domain.TerminalType;
import com.zfzs.post.modules.post.service.TerminalTypeService;
import com.zfzs.post.service.GoodsAboutService;
import com.zfzs.post.service.JwtUserDetailsService;
import com.zfzs.post.utils.CloneUtils;
import com.zfzs.post.utils.JsonUtils;
import com.zfzs.post.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wq
 * @date 2019/10/8 17:56
 * @description
 */
@Service
public class GoodsAboutServiceImpl implements GoodsAboutService {

    @Autowired
    private JwtUserDetailsService jwtUserDetailsService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private TerminalTypeService terminalTypeService;

    @Autowired
    private GoodOrderService goodOrderService;

    @Override
    public List<GoodsListVO> getGoodsList(Integer goodsType) {
        AgentDTO agentInfo = jwtUserDetailsService.getAgentInfo();
        Goods goods = new Goods();
        goods.setChannelId(agentInfo.getChannelId());
        goods.setIsShow(1);
        if (goodsType != null && goodsType.intValue() != 0) {
            goods.setGoodsType(goodsType);
        }
        List<Goods> list = goodsService.findList(goods);
        List<GoodsListVO> voList = new ArrayList<>();
        for (Goods g : list) {
            GoodsListVO v = CloneUtils.clone(g, GoodsListVO.class);
            v.setGoodsId(g.getId());
            if (g.getGoodsType() == 1) {
                TerminalType byId = terminalTypeService.findById(g.getTerminalTypeId());
                if (byId == null) {
                    throw new BadRequestException("未查询到机具类型");
                }
                v.setTerminalName(byId.getTerminalName());
            } else {
                v.setTerminalName("物料");
            }

            voList.add(v);
        }
        return voList;
    }

    @Override
    public GoodsVO getGoodsById(String id) {
        Goods g = goodsService.findById(id);
        if (g == null) {
            throw new BadRequestException("未查询到该商品");
        }
        GoodsVO v = CloneUtils.clone(g, GoodsVO.class);
        TerminalType byId = terminalTypeService.findById(g.getTerminalTypeId());
        if (byId == null) {
            throw new BadRequestException("未查询到机具类型");
        }
        v.setTerminalName(byId.getTerminalName());
        v.setGoodsId(g.getId());
        return v;
    }

    @Override
    public List<GoodsListVO> getGoodsByIds(List<String> ids) {
        List<Goods> list = goodsService.findByIds(ids);
        List<GoodsListVO> voList = new ArrayList<>();
        for (Goods g : list) {
            GoodsListVO v = CloneUtils.clone(g, GoodsListVO.class);
            v.setGoodsId(g.getId());
            TerminalType byId = terminalTypeService.findById(g.getTerminalTypeId());
            if (byId == null) {
                throw new BadRequestException("未查询到机具类型");
            }
            v.setTerminalName(byId.getTerminalName());
            voList.add(v);
        }
        return voList;
    }

    @Override
    public Goods findById(String id) {
        return goodsService.findById(id);
    }

    @Override
    public List<Map<String, String>> getGoodsType() {

        GoodsType[] values = GoodsType.values();
        List<Map<String, String>> list = new ArrayList<>();
        for (GoodsType t : values) {
            list.add(new HashMap<String, String>(2) {{
                put("goodsType", t.getValue().toString());
                put("name", t.getMsg());
            }});

        }
        return list;
    }

    @Override
    public Map<String,Object> getKuaiDiInfo(Map<String, String> params) {
        String orderNo = params.get("orderNo");
        if(StringUtils.isBlank(orderNo)){
            throw new BadRequestException("请输入订单号");
        }
        //查询订单号
        GoodOrder order = goodOrderService.findByOrderNo(orderNo);
        if(order == null){
            throw new BadRequestException("未查询到订单号");
        }
        if(StringUtils.isBlank(order.getExpressNo())){
            throw new BadRequestException("此订单还未添加快递信息");
        }
        String res = KuaiDiUtils.getKuaiDiInfo(order.getExpressNo(), null);
        return JsonUtils.fromJson(res);
    }
}
